import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
// import 'package:my_plugins/my_plugins.dart';
import 'package:time_manager/global/controller/record_item_controller.dart';
import 'package:time_manager/utils/DataBaseRecord.dart';
import 'package:time_manager/utils/RecordModel.dart';
import 'package:time_manager/utils/permission_handler.dart';
import 'package:time_manager/utils/timeDeal.dart';

class AddRecordController extends GetxController {
  RecordItemController recordController = Get.find<RecordItemController>();
  var currentTime = TimeUtil().getLocalTime(
      ((int.tryParse(TimeUtil().getTimeStamp())! + (24 * 60 * 60 * 1000))
          .toString()),
      format: 'hour');
  var time = ''.obs;
  final titleController = TextEditingController();
  final durationController = TextEditingController();
  var params = {};
  FocusNode commentFocus = FocusNode();

  @override
  void onInit() async {
    time(currentTime);
    super.onInit();
  }

  bindController() {}
  showDate() async {
    final datePick = await showDatePicker(
        context: Get.context!,
        firstDate: DateTime(2000),
        lastDate: DateTime(2100),
        locale: const Locale('zh'),
        initialDate: DateTime.now());
    if (datePick != null) {
      var _dateTime = datePick;
      var temp = TimeUtil().getLocalTime(
          _dateTime.millisecondsSinceEpoch.toString(),
          format: 'hour');
      time(temp);

      update([time]);
    }
  }

  confirm() async {
    final title = titleController.value.text;
    final duration = durationController.value.text;

    if (title == '' || duration == '') {
      Get.snackbar('提示', '日程和时长都不能为空',
          margin: const EdgeInsets.all(40),
          // 提示框距离四周的距离
          snackPosition: SnackPosition.TOP);
      return;
    }
    if (commentFocus.hasFocus) {
      commentFocus.unfocus();
    }

    RecoreModel params = RecoreModel(
        title: title,
        status: '1',
        duration: duration,
        dealtime: DateTime.parse(time.value).millisecondsSinceEpoch.toString());
    Get.defaultDialog(
      barrierDismissible: false,
      title: "提示", // 弹窗的标题
      onConfirm: () async {
        if (await MyPermissionHandler.checkCalender()) {
          var tipTime = DateTime.parse(time.value)
              .add(const Duration(hours: 12))
              .millisecondsSinceEpoch;
          // var res =
          //     await MyPlugins.sendCalender(tipTime, params.title, params.title);
        }
        await recordController.addRecord(params);
        Get.back();
        Get.back();
      }, // 确定回调
      onCancel: () {}, // 取消的回调
      content: const Text("您确定要提交吗？"),
    );
  }
}
